<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="author" content="金西振">
  <title></title>
  <style>
    body{
      margin: 0;
    }
    .box{
      width: 300px;
      height: 400px;
      background-color: rgb(255, 255, 255);
      position: absolute;
      top: calc(50% - 200px);
      left: calc(50% - 150px);
      display: none;
    }
    .cancel{
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 35px;
      cursor: pointer;
    }
    .head{
      height: 60px;
      background-color: #fff;
      border-bottom: 1px solid rgb(99, 99, 99);
    }
    .mask{
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color:rgba(0, 0, 0,0.5);
      display: none;
      /* z-index: -1; */
    }
    span{
      position: relative;
      bottom: 12px;
    }
    button{
      width: 50px;
      height: 30px;
    }
    p{
      font-size: 80px;
      color: rgb(236, 155, 155);
    }
    
  </style>
</head>
<body>
  <button class="btn">弹窗</button>
  <div class="mask"> 
    <div class="box">
      <div class="head">
        <span class="cancel">x</span>
      </div>
    </div> 
</div>
 
 <p>遮罩层</p>
<script>
  var box = document.querySelector('.box');
  var btn = document.querySelector('.btn');
  var mask = document.querySelector('.mask');
  var can = document.querySelector('.cancel');

  // 鼠标移动事件
  box.onmousedown = function(e){
    var q = e.pageX-box.offsetLeft;
    var s = e.pageY-box.offsetTop;
     box.style.cursor = 'move';
    document.onmousemove = function(ev){
      box.style.left = ev.pageX - q + 'px';
      box.style.top = ev.pageY - s +'px';
    }
  }

  box.onmouseup = function(){
    box.style.cursor = 'auto';
    document.onmousemove = null;
  }

  btn.onclick = function(){
    box.style.display = 'block';
    mask.style.display = 'block';
  }
  
  can.onmouseup = function(){
    box.style.display = 'none';
    mask.style.display = 'none';
    
  }


  
</script>
</body>
</html>